// src/utils/dingtalk.js
import * as dd from 'dingtalk-jsapi'

/**
 * 判断是否在钉钉环境中
 */
export function isDingTalkEnv() {
    return /DingTalk/i.test(navigator.userAgent)
}

// 判断是否PC端钉钉
export function isDingTalkPC() {
    return /dingtalk/i.test(navigator.userAgent) && !/Mobile/i.test(navigator.userAgent)
}

/**
 * 获取企业员工免登授权码
 * @param {string} corpId 企业 corpId
 * @returns Promise<string> 返回 code
 */
export function getAuthCode(corpId) {
    return new Promise((resolve, reject) => {

        dd.ready(() => {
            dd.runtime.permission.requestAuthCode({
                corpId,
                onSuccess: function(info) {
                    resolve(info.code)
                },
                onFail: function(err) {
                    reject(err)
                }
            })
        })

        dd.error((err) => reject(err))
    })
}
